﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Web.UI;
using Terralever.Facebook.UI.FbmlControls;
using System.ComponentModel.Design;

namespace Terralever.Facebook.UI.Design.FbmlControls
{
    public class ContentDisplayControlDesigner : FbmlControlDesigner
    {
        #region region class
        private class LogicTestControlDesignerRegion : TemplatedEditableDesignerRegion
        {
            private object m_obj;
            private PropertyDescriptor m_pd;
            private ITemplate m_template;

            public LogicTestControlDesignerRegion(object obj, ITemplate template, PropertyDescriptor descriptor, TemplateDefinition definition)
                : base(definition)
            {
                m_obj = obj;
                m_template = template;
                m_pd = descriptor;
                base.EnsureSize = true;
            }

            public object Object { get { return m_obj; } }
            public ITemplate Template { get { return m_template; } set { m_template = value; } }
            public PropertyDescriptor PropertyDescriptor { get { return m_pd; } }
        }
        #endregion

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            LogicTestControlDesignerRegion rgn = region as LogicTestControlDesignerRegion;
            if (rgn != null)
            {
                ITemplate template = rgn.Template;
                if (template != null)
                {
                    IDesignerHost svc = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                    return ControlPersister.PersistTemplate(template, svc);
                }
            }
            return base.GetEditableDesignerRegionContent(region);
        }

        /// <inheritdoc />
        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            LogicTestControlDesignerRegion rgn = region as LogicTestControlDesignerRegion;
            if (rgn != null)
            {
                IDesignerHost svc = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
                ITemplate template = ControlParser.ParseTemplate(svc, content);
                rgn.PropertyDescriptor.SetValue(rgn.Object, template);
                rgn.Template = template;
            }
        }

        /// <inheritdoc />
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);
        }

        /// <inheritdoc />
        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            ContentDisplayControl ltc = Component as ContentDisplayControl;
            if (regions == null)
                return base.GetDesignTimeHtml(regions);

            EditableDesignerRegion region = new LogicTestControlDesignerRegion(Component, ltc.ContentTemplate, TypeDescriptor.GetProperties(Component)["ContentTemplate"], ContentTemplateDefinition);
            region.Description = "The template displayed to users who meet the requirements of viewing this control.";
            regions.Add(region);

            return "<div _designerRegion=\"0\"></div>";
        }

        /// <inheritdoc />
        protected override bool UsePreviewControl
        {
            get
            {
                return true;
            }
        }

        private TemplateGroupCollection m_templateGroups;

        /// <inheritdoc />
        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                if (m_templateGroups == null)
                {
                    m_templateGroups = base.TemplateGroups;
                    TemplateGroup group = new TemplateGroup("All Templates");
                    group.AddTemplateDefinition(ContentTemplateDefinition);
                    m_templateGroups.Add(group);
                }

                return m_templateGroups;
            }
        }

        private TemplateDefinition m_tdContent;
        private TemplateDefinition ContentTemplateDefinition
        {
            get
            {
                if (m_tdContent == null)
                    m_tdContent = new TemplateDefinition(this, "Displayed Content Template", Component, "ContentTemplate", false);
                return m_tdContent;
            }
        }

        public override bool AllowResize
        {
            get
            {
                return InTemplateMode;
            }
        }
    }
}
